/**
 * Created by oneminute on 7/15/14.
 */
var assert = require("assert");
var should = require("should");
var logger = require("../lib/utils/logger");
var testutils = require("./testutils");
var lz = require("../lib/utils/modules")();

describe("测试user模块。", function () {
    var _id = null;
    var user = {
        id: "t_100000",
        email: "t_100000@lz.com"
    };
    var newUser = null;

    it("使用createNew方法成功创建user对象。", function (done) {
        lz.User.createNew(user, function (_err, _user) {
            testutils.check(done, function () {
                _id = _user._id;
                _user.id.should.equal(user.id);
            });
        });
    });

    it("使用create方法成功创建user对象，并使用save方法保存全部属性。", function(done) {
        var tmpUser = {};
        tmpUser.id = "t_100001";
        tmpUser.email = "t_100001@lz.com";
        newUser = lz.User.create(tmpUser);
        newUser.save({all: true}, function(_err, _user) {
            testutils.check(done, function() {
                assert.equal(newUser, _user);
                _user.id.should.equal(tmpUser.id);
                assert.notEqual(newUser.email._id, null);
            });
        });
    });

    it("Get user instance succesfully.", function (done) {
        lz.User.loadByAId(_id, function (_err, _user) {
            testutils.check(done, function () {
                _user.id.should.equal(user.id);
            });
        });
    });

    it("Get user instance not existed.", function (done) {
        lz.User.loadByAId(_id + 1, function (_err, _user) {
            testutils.check(done, function () {
                assert.notEqual(_err, null);
            });
        });
    });

    it("Delete user instance by arangodb id.", function (done) {
        lz.User.deleteByAId(_id, function (_err) {
            var db = lz.ArangoDB.getConnection();
            db.document.head(_id, function (_err, _ret, _message) {
                testutils.check(done, function () {
                    assert.equal(_err, -1);
                    assert.equal(_message.status, 404);
                });
            });
        });
    });

    after(function(done) {
        lz.User.deleteByAId(newUser._id, function () {
            done();
        });
    });
});